package com.xt.partjobback.utils;


import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;

/**
 * @author leotong
 */
public class TokenUtil {

    /**
     * 加密工具
     */
    private static final BASE64Encoder ENCODER =new BASE64Encoder();

    /**
     * 解密工具
     */
    private static final BASE64Decoder DECODER = new BASE64Decoder();

    /**
     * 创建token
     * @return token
     */
    public static  String createToken(String value){
        return ENCODER.encode(value.getBytes());
    }

    /**
     * 从token提取ID
     * @param token token
     * @return id
     */
    public static Integer tokenExtractId(String token){
        byte[] bytes = new byte[0];
        try {
            bytes = DECODER.decodeBuffer(token);
            String s = new String(bytes);
            return Integer.parseInt(s.substring(s.indexOf(":")+1));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
